<template>
  <view>
    <view class="address-btn" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" @click="chooseAddress">请点击添加地址</button>
    </view>
    <view class="address-info" v-else @click="chooseAddress">
      
      <view class="address-rowl1">
        <view class="address-name">
          收货人：<text>{{address.userName}}</text>
        </view>
        <view class="address-phone">
          联系电话：<text>{{address.telNumber}}</text>
        </view>
      </view>
      <view class="address-rowl2">
        <view class="address-left">
          收货地址：
        </view>
        <view class="address-right">
          {{addstr}}
        </view>
      </view>
      <uni-icons type="forward" size="12" class="address-arrow"></uni-icons>
    </view>
  </view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
  export default {
    name:"my-address",
    data() {
      return {
        // address:{}
      };
    },
    methods:{
      ...mapMutations('m_user',['updateAddress']),
      async chooseAddress(){
        const [err,succ] = await uni.chooseAddress().catch(err => err)
        
        if(err === null && succ.errMsg === 'chooseAddress:ok'){
          this.updateAddress(succ)
        }
        if(err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')){
          this.reAuth()
        }
      },
      async reAuth(){
        const [err2,confirmResult] = await uni.showModal({
          content:'检测到您未打开地址权限，是否设置开启',
          confirmText:'确认',
          cancelText:'取消'
        })
        if(err2) return
        
        if(confirmResult.cancel) return uni.$showMsg('您取消了授权！')
        
        if(confirmResult.confirm) return uni.openSetting({
          success: (settingResult) => {
            if(settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
            if(!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
          }
        })
      }
    },
    computed:{
      ...mapState('m_user',['address']),
      ...mapGetters('m_user',['addstr'])
    }
  }
</script>

<style lang="scss">
.address-btn{
  height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.address-info{
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  height: 90px;
  padding: 0 5px;
  font-size: 14px;
  margin-right: 10px;
  .address-rowl1{
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .address-rowl2{
    margin-top: 10px;
    display: flex;
    align-items: center;
    
    .address-left{
      white-space: nowrap;
    }
  }
  .address-arrow{
    position: absolute;
    right: 5px;
    top: 39px;
  }
}
</style>